package com.xiaoyu.commonlib.plug;

import android.util.SparseArray;

public class PlugManager {
    public static final int MASTER_ID = 1;

    private volatile static PlugManager sInstance;

    public static PlugManager getInstance(){
        if (sInstance == null){
            synchronized (PlugManager.class){
                sInstance = new PlugManager();
            }
        }
        return sInstance;
    }

    private final SparseArray<IPlugRules> mPlugArrays = new SparseArray<>();

    /**
     * 添加插件接口
     * @param plugID 插件标识
     * @param plugRules
     */
    public void addPlugRules(int plugID,IPlugRules plugRules){
        if (mPlugArrays.indexOfKey(plugID) < 0){
            mPlugArrays.put(plugID,plugRules);
        }
    }


    /**
     * 获取插件接口根据类型
     * @param plugID
     * @return
     */
    public <T> T getPlugRulesByType(int plugID,Class<T> cls){
        IPlugRules rules = mPlugArrays.get(plugID);
        if (rules != null){
            return (T) rules;
        }
        return null;
    }
}
